#ifndef HJX_H_INCLUDED
#define HJX_H_INCLUDED



#endif // HJX_H_INCLUDED
namespace hjx
{
    template<class T>
    class vector
    {
    public:
        typedef T* iterator;
        typedef const T* const_iterator;
        vector()
        :_start(nullptr)
        ,_finish(nullptr)
        ,_end(nullptr)
        {

        }
        //拷贝构造写法一
        //v2(v1);
//        vector(const vector<T>&v)
//        {
//            _start=new T[v.capacity()];
//            _finish=_start;
//            _end=_start+v.capacity();
//            for(size_t i=0;i<v.size();++i)
//            {
//                *_finish=v[i];
//                _finish++;
//            }
//        }
//        拷贝构造写法二
         //v2(v1)
         vector(const vector<T>&v)
         :_start(nullptr)
         ,_finish(nullptr)
         ,_end(nullptr)
         {
             reserve(v.capacity());
             for(auto e:v)
             {
                 push_back(e);
             }
         }
        ~vector()
        {
            delete[]_start;
            _finish=_start=_end=nullptr;
        }
        const iterator begin()const
        {
            return _start;
        }
        const iterator end()const
        {
            return _finish;
        }
        iterator begin()
        {
            return _start;
        }
        iterator end()
        {
            return _finish;
        }
        void reserve(size_t n)
        {
           if(n>capacity())//需要扩容
           {
               int sz=size();
               T*tmp=new T[n];
               if(_start!=nullptr)
               {
                //memcpy(tmp,_start,sizeof(T)*size());//浅拷贝,按字节拷贝
                for(size_t i=0;i<sz;i++)
                {
                    tmp[i]=_start[i];
                }
                delete []_start;
               }
               _start=tmp;
               _finish=tmp+sz;
               _end=tmp+n;
           }
        }
        void push_back(const T&n)
        {
//            if(_finish==_end)
//            {
//                size_t newcapacity=capacity()==0?2:2*capacity();
//                reserve(newcapacity);
//            }
//            *_finish=n;
//            _finish++;
//尾插可以调用insert函数
           insert(_finish,n);
        }
        T& operator[](size_t i)
        {
            if(i<size())
            {
                return _start[i];
            }

        }
        const T&operator[](size_t i)const
        {
            if(i<size())
            {
                 return _start[i];
            }

        }
        size_t size()const
        {
            return _finish-_start;
        }
        size_t capacity()const
        {
            return _end-_start;
        }
        void pop_back()//尾删
        {
//            if(_start<_finish)//断言会更好assert
//            {
//                _finish--;
//            }
//尾删可以套用erase
              erase(_finish-1);
        }
        void insert(iterator pos,const T&x)//任意位置插入
        {
            if(pos<=_finish)
            {
                if(_finish==_end)
            {
                size_t n=pos-_start;
                int newcapacity=capacity()==0?2:2*capacity();
                reserve(newcapacity);
                pos=_start+n;
            }
            iterator end1=_finish-1;
            while(pos<=end1)
            {
                *(end1+1)=*end1;
                end1--;
            }
            *pos=x;
            _finish++;
            }
        }
        iterator erase(iterator pos)//任意位置删除
        {
            if(pos<_finish)
            {
                iterator it=pos;
                while(it<_finish)
                {
                    *it=*(it+1);
                    it++;
                }
                _finish--;
            }
            return pos;
        }
        void resize(size_t n,const T& val=T())//开空间
        {
            if(n<size())
            {
                _finish=_start+n;
            }
            else
            {
                if(n>capacity())
                {
                    reserve(n);//增容
                }
                while(_finish<_start+n)
                {
                    *_finish=val;
                    _finish++;
                }
            }
        }
        //v2=v1;
        vector<T>& operator=(vector<T> v)
        {
            if(this!=&v)//防止自己给自己赋值
            {
                swap(v);
                return *this;
            }
        }
        void swap(vector<T>& v)
        {
            ::swap(_start,v._start);
            ::swap(_finish,v._finish);
            ::swap(_end,v._end);
        }
    private:
        iterator _start;
        iterator _finish;
        iterator _end;
    };
    void text1()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        vector<int>::iterator it=v.begin();
        while(it!=v.end())
        {
            cout<<*it<<" ";
            it++;
        }
        cout<<endl;
        //范围for
        for(auto& e:v)
        {
//            e-=1;
            cout<<e<<" ";
        }
        cout<<endl;
        //operator遍历
        for(size_t i=0;i<v.size();i++)
        {
            cout<<v[i]<<" ";
        }
    }
    void print_hjx(const vector<int>&v)
    {
        vector<int>::const_iterator it=v.begin();
        while(it!=v.end())
        {
//            *it+=1;//只读所以会报错
            cout<<*it<<" ";
            it++;
        }

    }
    void text2()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        print_hjx(v);//只读
    }
    void text3()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        print_hjx(v);//只读
        cout<<endl;
        v.insert(v.begin(),0);
        print_hjx(v);//只读
        cout<<endl;
        v.erase(v.begin());
        print_hjx(v);
        cout<<endl;
    }
    void text4()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        cout<<v.size()<<endl;
        cout<<v.capacity()<<endl;
        v.resize(13,9);
        cout<<v.size()<<endl;
        cout<<v.capacity()<<endl;
    }
    void text5()
    {
        vector<int> v;
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
        vector<int> v1(v);//拷贝构造
        print_hjx(v1);
        cout<<endl;
        vector<int> v2;
        v2=v;//赋值
        print_hjx(v2);
    }
    void text6()//深拷贝更深的理解
    {
        vector<string> s;
        s.push_back("1111111111");
        s.push_back("2222222222");
        s.push_back("3333333333");
        for(size_t i=0;i<s.size();i++)
        {
            cout<<s[i]<<endl;
        }
    }
}

















